<?php

function greengar_count_favorite($userid, $app, $favorite = TRUE) {
    global $CONFIG;
    $relationship = greengar_get_relationship_favorite($app, $favorite);
    $relationship_query = "select count(guid_two) as total from {$CONFIG->dbprefix}entity_relationships where guid_one = $userid and relationship = '$relationship'";
    try {
        $total = get_data_row($relationship_query);
        return (int)$total->total;
    } catch (DatabaseException $de) {
        
    }
    return 0;
}

function greengar_count_favorite_reverse($userid, $app, $favorite = TRUE) {
    global $CONFIG;
    $relationship = greengar_get_relationship_favorite($app, $favorite);
    $relationship_query = "select count(guid_one) as total from {$CONFIG->dbprefix}entity_relationships where guid_two = $userid and relationship = '$relationship'";
    try {
        $total = get_data_row($relationship_query);
        return (int)$total->total;
    } catch (DatabaseException $de) {
        
    }
    return 0;
}

function greengar_get_favorite($userid, $app, $favorite = TRUE) {
    global $CONFIG;
    $relationship = greengar_get_relationship_favorite($app, $favorite);
    $relationship_query = "select guid_two from {$CONFIG->dbprefix}entity_relationships where guid_one = $userid and relationship = '$relationship'";
    try {
        //echo $relationship_query;
        $rows = get_data($relationship_query);
        //var_dump($rows);
        $resultarray = array();
        foreach($rows as $row) {
            $guid = $row->guid_two;
            $entity = get_entity($guid);
            $resultarray[] = WhiteboardDrawing::toArray($entity);
        }
        return $resultarray;
    } catch (DatabaseException $de) {
        
    }
    return array();
}

function greengar_get_favorite_reverse($userid, $app, $favorite = TRUE) {
    global $CONFIG;
    $relationship = greengar_get_relationship_favorite($app, $favorite);
    $relationship_query = "select guid_one from {$CONFIG->dbprefix}entity_relationships where guid_two = $userid and relationship = '$relationship'";
    try {
        $rows = get_data($relationship_query);
        $resultarray = array();
        foreach($rows as $row) {
            $guid = $row->guid_one;
            $entity = get_entity($guid);
            $resultarray[] = WhiteboardDrawing::toArray($entity);
        }
        return $resultarray;
    } catch (DatabaseException $de) {
        
    }
    return array();
}

?>
